package tables

import (
	"gorm.io/gorm"
	"time"
)

type Goods struct {
	GoodsID      string         `gorm:"column:goods_id;type:varchar(10);primary_key"`
	GoodsDesc    GoodsDesc      `gorm:"foreignKey:GoodsID"`
	CategoryID   int32          `gorm:"column:category_id;type:int;not null;index;"`
	Category     Category       `gorm:"foreignKey:CategoryID"`
	Name         string         `gorm:"column:goods_name;type:varchar(80);not null"`
	Desc         string         `gorm:"column:goods_desc;type:varchar(800);not null"`
	Click        int32          `gorm:"column:goods_click;type:int;not null;default:0"`
	Score        int32          `gorm:"column:goods_score;type:tinyint;not null;"`
	FactoryPrice int32          `gorm:"column:factory_price;type:int;not null;index;commit:'所有价格*100, 防止小数点精确错误, 这是工厂报价'"`
	OutMinPrice  int32          `gorm:"column:goods_min_price;type:int;not null;index;commit:'所有价格*100, 防止小数点精确错误, 这是第一个价格'"`
	OutMaxPrice  int32          `gorm:"column:goods_max_price;type:int;not null;index;commit:'所有价格*100, 防止小数点精确错误, 这是第一个价格'"`
	PackingMode  string         `gorm:"column:goods_packing_mode;type:varchar(20);not null;default:'opp'"`
	LeadTime     string         `gorm:"column:goods_lead_time;type:varchar(10);not null;default:'7-9'"`
	DeliveryTime string         `gorm:"column:goods_delivery_time;type:varchar(10);not null;default:'4-7"`
	MainPicture  string         `gorm:"column:goods_main_picture;type:varchar(300);not null"`
	CreatedAt    *MyTime        `gorm:"column:create_time"`
	UpdatedAt    *MyTime        `gorm:"column:update_time"`
	DeleteAt     gorm.DeletedAt `gorm:"index"`
	IsNew        int32          `gorm:"column:goods_is_new;type:tinyint;not null;index"`
	IsHot        int32          `gorm:"column:goods_is_hot;type:tinyint;not null;index"`
	KeyWords     JSON           `gorm:"column:key_words;type:json;not null"`
}

func (g *Goods) TableName() string {
	return "goods"
}

// 钩子函数
func (g *Goods) BeforeCreate(tx *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	g.CreatedAt = &MyTime{Time: fptTime}
	g.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}

func (g *Goods) BeforeUpdate(scope *gorm.DB) error {
	t := time.Now().UTC().Format(FormatTime)
	fptTime, err := time.ParseInLocation(FormatTime, t, time.Local)
	if err != nil {
		return err
	}
	g.UpdatedAt = &MyTime{Time: fptTime}
	return nil
}

type GoodsDesc struct {
	GoodsID       string   `gorm:"column:goods_desc_id;type:varchar(10);index"`
	Color         string   `gorm:"column:goods_color;type:varchar(200)"`
	ProductLength int32    `gorm:"column:goods_product_length;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	ProductWidth  int32    `gorm:"column:goods_product_width;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	ProductHeight int32    `gorm:"column:goods_product_height;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	ProductWeight int32    `gorm:"column:goods_product_weight;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BoxLength     int32    `gorm:"column:goods_box_length;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BoxWidth      int32    `gorm:"column:goods_box_width;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BoxHeight     int32    `gorm:"column:goods_box_height;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BoxNumber     int32    `gorm:"column:goods_box_number;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BoxWeight     int32    `gorm:"column:goods_box_weight;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	Material      string   `gorm:"column:goods_material;type:varchar(200)"`
	Image         GormList `gorm:"column:goods_image;type:json;not null"`

	Qty1   int32 `gorm:"column:goods_qty1;type:int;"`
	Price1 int32 `gorm:"column:goods_price1;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty2   int32 `gorm:"column:goods_qty2;type:int;"`
	Price2 int32 `gorm:"column:goods_price2;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty3   int32 `gorm:"column:goods_qty3;type:int;"`
	Price3 int32 `gorm:"column:goods_price3;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty4   int32 `gorm:"column:goods_qty4;type:int;"`
	Price4 int32 `gorm:"column:goods_price4;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty5   int32 `gorm:"column:goods_qty5;type:int;"`
	Price5 int32 `gorm:"column:goods_price5;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty6   int32 `gorm:"column:goods_qty6;type:int;"`
	Price6 int32 `gorm:"column:goods_price6;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty7   int32 `gorm:"column:goods_qty7;type:int;"`
	Price7 int32 `gorm:"column:goods_price7;type:int;commit:'所有价格*100, 防止小数点精度问题'"`

	Qty8   int32 `gorm:"column:goods_qty8;type:int;"`
	Price8 int32 `gorm:"column:goods_price8;type:int;commit:'所有价格*100, 防止小数点精度问题'"`
	BaseModel
}

func (gd *GoodsDesc) TableName() string {
	return "goods_desc"
}
